package com.sky.service.user.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.exception.WeChatLoginOpenIdInvalidException;
import com.sky.mapper.common.MerchantMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.pojo.Merchant;
import com.sky.pojo.User;
import com.sky.properties.JwtProperties;
import com.sky.properties.WeChatProperties;
import com.sky.service.user.UserService;
import com.sky.utils.JwtUtil;
import com.sky.utils.MapUtils;
import com.sky.utils.WeChatAuthUtil;
import com.sky.vo.UserLoginVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户业务实现
 * @author zgp
 * @version V1.0
 * @date 2025-01-24 14:22
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final MerchantMapper merchantMapper;
    private final WeChatProperties weChatProperties;
    private final JwtProperties jwtProperties;
    private final WeChatAuthUtil weChatAuthUtil;


    @Override
    public UserLoginVO login(UserLoginDTO userLoginDTO) {
        // 1.根据临时登录凭证 code 获取openid，调用wx第三方登录接口获取
        String openid = weChatAuthUtil.getOpenid(userLoginDTO.getCode());
        if(StrUtil.isEmpty(openid)){
            throw new WeChatLoginOpenIdInvalidException(MessageConstant.WECHAT_LOGIN_FAILD_OPENID_NOT_EXIST);
        }
        // 判断登录用户是否为新用户
        User user = userMapper.selectByOpenid(openid);
        if(ObjectUtil.isNull(user)){
            // 新用户，新增到用户表
            user = new User();
            user.setOpenid(openid);
            user.setCreateTime(LocalDateTime.now());
            userMapper.insert(user);
        }
        // 生成用户端token
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        // 查询商家信息
        Merchant merchant = merchantMapper.selectById(1L);
        // 计算配送费：首公里3元，后续每公里加1元，不足1公里按照1公里计费
        // step1: 计算商家和用户两者间得距离
        // step2: 计算运费
        BigDecimal delivoryCost = MapUtils.getDelivoryCost(merchant.getLocation(), userLoginDTO.getLocation());
        // 构建登录成功返回实体数据
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setId(user.getId());
        userLoginVO.setOpenid(openid);
        userLoginVO.setToken(token);
        userLoginVO.setShopName(merchant.getShopName());
        userLoginVO.setShopAddress(merchant.getShopAddress());
        userLoginVO.setShopTelephone(merchant.getShopTelephone());
        userLoginVO.setDeliveryFee(delivoryCost);
        userLoginVO.setShopId(merchant.getId());
        // 返回数据
        return userLoginVO;
    }

}
